{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b68402e9",
   "metadata": {},
   "source": [
    "------------------\n",
    "* 2022/9/19/week05  \n",
    "* python学习记录  \n",
    "----------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99f127f3",
   "metadata": {},
   "source": [
    "# 本周知识点  \n",
    "\n",
    "\n",
    "> 1.控制流  \n",
    ">> A.条件判断  \n",
    ">> B.循环遍历  \n",
    "\n",
    "> 2.随机函数  \n",
    "\n",
    "> 3.时间函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6644be9",
   "metadata": {},
   "source": [
    "## 1.控制流  \n",
    "\n",
    "### 1.1条件判断  \n",
    "\n",
    "* 知识点  \n",
    "> 1.赋值 是 \"=\"  \n",
    "> 2.判断是否两者相等 是 \"=\"  \n",
    "> 3.代码中出现的变量,必须先定义,否则: name 变量名 is not defind\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2cd90b2b",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (1631535108.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [1]\u001b[1;36m\u001b[0m\n\u001b[1;33m    if today =\"Saturday\":\u001b[0m\n\u001b[1;37m             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "if today =\"Saturday\":\n",
    "    print(\"party\")\n",
    "elif today =\"Sunday\":\n",
    "    if condition = (\"Recover,then rest.\")\n",
    "    else:\n",
    "        print(\"Rest.\")\n",
    "else:\n",
    "    print(\"Mork,work,work.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2d885175",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 先定义变量  \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bc2bf4e9",
   "metadata": {},
   "outputs": [
    {
     "ename": "IndentationError",
     "evalue": "unindent does not match any outer indentation level (<tokenize>, line 9)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m<tokenize>:9\u001b[1;36m\u001b[0m\n\u001b[1;33m    else:\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m unindent does not match any outer indentation level\n"
     ]
    }
   ],
   "source": [
    "today = \"Monday\"\n",
    "condition = \"Headache\"\n",
    "\n",
    "if today == \"Saturday\":\n",
    "    print(\"Party!\")\n",
    "elif today == \"Sunday\":\n",
    "     if condition == \"Headache\":\n",
    "            print(\"Recover,then rest.\")\n",
    "    else: \n",
    "        print(\"Rest.\")\n",
    "else:\n",
    "    print(\"Work,work,work.\")\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b5d17a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 关于if else 条件判断实践:  \n",
    "## 请判断今天是周几,如果是周一,有Python课,好开心啊,如果不是,期待周一呀.(time可以帮助我们知道今天周几)  \n",
    "##                   如果是周一,请判断现在是否正在上Pytodathon课               "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7c0cc8ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "0e97152c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2022, tm_mon=9, tm_mday=19, tm_hour=10, tm_min=28, tm_sec=6, tm_wday=0, tm_yday=262, tm_isdst=0)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "30674aa7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime().tm_wday"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "099a3686",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "周几 0\n",
      "几点 11\n",
      "几分 21\n"
     ]
    }
   ],
   "source": [
    "# tm_day 0-6 0 是周一\n",
    "wday = time.localtime().tm_wday\n",
    "print(\"周几\",wday)\n",
    "+++++++++\n",
    "today_hour = time.localtime().tm_hour\n",
    "print(\"几点\",today_hour)\n",
    "today_min = time.localtime().tm_min\n",
    "print(\"几分\",today_min)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "1f26a466",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "今天是周一,有Python课,好开心啊\n",
      "现在是 10 点 55 分 正在上Python\n"
     ]
    }
   ],
   "source": [
    "\n",
    "if wday == 0:\n",
    "    print(\"今天是周一,有Python课,好开心啊\")\n",
    "    if 9 < today_hour < 12:\n",
    "        print(\"现在是\",today_hour,\"点\",today_min,\"分\",\"正在上Python\")\n",
    "    elif today_hour == 9:\n",
    "        if today_min >= 45:\n",
    "            print(\"正在上Python\")\n",
    "        else:\n",
    "            print(\"马上就要上Python了,好激动呀 \")\n",
    "    else:\n",
    "        print(\"没有上Pyhthon课\")\n",
    "else:\n",
    "    print(\"期待周一呀\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd87ec69",
   "metadata": {},
   "source": [
    "## 2.循环遍历(for)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "efa881ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in [1,2,3]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "cf354bf6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H\n",
      "i\n",
      "!\n"
     ]
    }
   ],
   "source": [
    "for ch in \"Hi!\":\n",
    "    print(ch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "13761452",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 上述 i 和 ch 都是变量,可以根据我们的需求定义变量名\n",
    "# 每一次执行,赋值一次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "7702749f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world!\n",
      "hello world!\n",
      "hello world!\n",
      "hello world!\n",
      "hello world!\n",
      "hello world!\n",
      "hello world!\n",
      "hello world!\n",
      "hello world!\n",
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "# range():范围,区间\n",
    "for num in range(10):\n",
    "    print(\"hello world!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "81ae1d49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 5)"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.range()是一个数字/数值生成器,默认每次增加1\n",
    "# 2.range() 包含有 起始点start(默认为0)   终止点 stop 以及步长step(默认为1)  \n",
    "\n",
    "range(5) # 默认的起点 是 0 , 终结点 5     产生了   5 - 0 个数值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "e9f1eaa8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 1, 2, 3, 4]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "d15ecdec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(5, 10)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(5,10) # 自定义起始点   5,终结点   10   产生了  10 - 5个数值,每次增加1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "8cbd2e96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 6, 7, 8, 9]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(5,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "8fb3b378",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 10, 3)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(0,10,3) # 自定义起始点  0,终结点  10  自定义每次增加3 (10-0)/3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "e66a252a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 3, 6, 9]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(0,10,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "0d63a2ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请参考以下,获得哔哩哔哩"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "35af242d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.bilibili.com/0.htm\n",
      "https://www.bilibili.com/1.htm\n",
      "https://www.bilibili.com/2.htm\n",
      "https://www.bilibili.com/3.htm\n",
      "https://www.bilibili.com/4.htm\n",
      "https://www.bilibili.com/5.htm\n",
      "https://www.bilibili.com/6.htm\n",
      "https://www.bilibili.com/7.htm\n",
      "https://www.bilibili.com/8.htm\n",
      "https://www.bilibili.com/9.htm\n",
      "https://www.bilibili.com/10.htm\n",
      "https://www.bilibili.com/11.htm\n",
      "https://www.bilibili.com/12.htm\n",
      "https://www.bilibili.com/13.htm\n",
      "https://www.bilibili.com/14.htm\n",
      "https://www.bilibili.com/15.htm\n",
      "https://www.bilibili.com/16.htm\n",
      "https://www.bilibili.com/17.htm\n",
      "https://www.bilibili.com/18.htm\n",
      "https://www.bilibili.com/19.htm\n",
      "https://www.bilibili.com/20.htm\n",
      "https://www.bilibili.com/21.htm\n",
      "https://www.bilibili.com/22.htm\n",
      "https://www.bilibili.com/23.htm\n",
      "https://www.bilibili.com/24.htm\n",
      "https://www.bilibili.com/25.htm\n",
      "https://www.bilibili.com/26.htm\n",
      "https://www.bilibili.com/27.htm\n",
      "https://www.bilibili.com/28.htm\n",
      "https://www.bilibili.com/29.htm\n",
      "https://www.bilibili.com/30.htm\n",
      "https://www.bilibili.com/31.htm\n",
      "https://www.bilibili.com/32.htm\n",
      "https://www.bilibili.com/33.htm\n",
      "https://www.bilibili.com/34.htm\n",
      "https://www.bilibili.com/35.htm\n",
      "https://www.bilibili.com/36.htm\n"
     ]
    }
   ],
   "source": [
    "# 实践:  数据挖掘---获取页面URL队列\n",
    "学校要闻 = \"xxyw\"\n",
    "通知公告 = \"tzgg\"\n",
    "\n",
    "for page in range(37):\n",
    "     print(\"https://www.bilibili.com/\"+ str(page) + \".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e0545497",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 练习,课本p4,涉及知识点:模块,变量,条件判断\n",
    "\n",
    "# 练习,课本p33 涉及知识点:模块,变量,条件判断,time模块,for循环\n",
    "\n",
    "# 练习,课本p44 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f88ac8b6",
   "metadata": {},
   "source": [
    "# P4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8883285a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This minute seems a little odd.\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "odds = [ 1, 3, 5, 7, 9,11,13,15,17,19,\n",
    "        21,23,25,27,29,31,33,35,37,39,\n",
    "        41,43,45,47,49,51,53,55,57,59 ]\n",
    "\n",
    "right_this_miunte = datetime.today().minute\n",
    "\n",
    "if right_this_miunte in odds:\n",
    "    print(\"This minute seems a little odd.\")\n",
    "else:\n",
    "    print(\"Not an odd minute.\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55b48f24",
   "metadata": {},
   "source": [
    "# P33"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ee4bfbcd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Not an odd minute.\n",
      "Not an odd minute.\n",
      "This minute seems a little odd.\n",
      "This minute seems a little odd.\n",
      "This minute seems a little odd.\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "import random\n",
    "import time\n",
    "\n",
    "odds = [ 1, 3, 5, 7, 9,11,13,15,17,19,\n",
    "        21,23,25,27,29,31,33,35,37,39,\n",
    "        41,43,45,47,49,51,53,55,57,59 ]\n",
    "\n",
    "for i in range(5):\n",
    "    right_this_miunte = datetime.today().minute\n",
    "    if right_this_miunte in odds:\n",
    "        print(\"This minute seems a little odd.\")\n",
    "    else:\n",
    "        print(\"Not an odd minute.\")\n",
    "    wait_time = random.randint(1,60)\n",
    "    time.sleep(wait_time)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73e70d95",
   "metadata": {},
   "source": [
    "# P44"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "2216770c",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'float' object cannot be interpreted as an integer",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [4]\u001b[0m, in \u001b[0;36m<cell line: 3>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m word \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mbottles\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m beer_num \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28;43mrange\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m99\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m0.\u001b[39;49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m:\n\u001b[0;32m      5\u001b[0m     \u001b[38;5;28mprint\u001b[39m(beer_num,word,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mof beer on the wall.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m      7\u001b[0m     \u001b[38;5;28mprint\u001b[39m(beer_num,word,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mof beer.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "\u001b[1;31mTypeError\u001b[0m: 'float' object cannot be interpreted as an integer"
     ]
    }
   ],
   "source": [
    "word = \"bottles\"\n",
    "\n",
    "for beer_num in range(99,0.-1):\n",
    "    \n",
    "    print(beer_num,word,\"of beer on the wall.\")\n",
    "    \n",
    "    print(beer_num,word,\"of beer.\")\n",
    "    \n",
    "    print(\"Take one down.\")\n",
    "    \n",
    "    print(\"Pass it around.\")\n",
    "    \n",
    "    if beer_num == 1:\n",
    "        \n",
    "        print(\"No more bottles of beer on the wall.\")\n",
    "        \n",
    "    else:\n",
    "        \n",
    "        new_num = beer_num - 1\n",
    "        \n",
    "        if new_num == 1:\n",
    "            \n",
    "            word = \"bottle\"\n",
    "            \n",
    "        print(new_num,word,\"of beer on the wall.\")\n",
    "        \n",
    "    print()\n",
    "        "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
